package receivers;

import tw.edu.tku.im.maze.RepPacket;
import tw.edu.tku.im.maze.StartGameRep;
import tw.edu.tku.im.maze.StartGameRep;
import tw.edu.tku.im.maze.TimeOutRep;

import com.example.maze.LobbyActivity;
import com.example.maze.MazeActivity;
import com.example.maze.RoomActivity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartGameRepReceiver extends BroadcastReceiver {
   Activity currentUIActivity;
   
   public StartGameRepReceiver(Activity activity) {
      currentUIActivity= activity;
   }

   @Override
   public void onReceive(Context context, Intent intent) {
      ((RoomActivity)currentUIActivity).dismissMyDialog();
      RepPacket rep= intent.getParcelableExtra("packet");
      if(rep instanceof TimeOutRep) {
         ((LobbyActivity)currentUIActivity).showMsgDialog("連線逾時");
      } else {
         StartGameRep mStartGameRep= (StartGameRep)rep;
         Intent mIntent= new Intent(currentUIActivity, MazeActivity.class);
         mIntent.putExtra("m", mStartGameRep.getMap().getM());
         mIntent.putExtra("n", mStartGameRep.getMap().getN());
         mIntent.putExtra("STEdges", mStartGameRep.getMap().getEdges());
         
         String ss="";
         for(int i=0; i<mStartGameRep.getMap().getEdges().length; i++) {
            ss += " "+ mStartGameRep.getMap().getEdges()[i];
         }
         android.util.Log.d("DebugMsg", ss);
         
         currentUIActivity.startActivity(mIntent);
      }
   }

}
